package com.bishe.tax.controller.sys;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxDeclaration;
import com.bishe.tax.entity.TaxRegistration;
import com.bishe.tax.entity.Taxpayer;
import com.bishe.tax.service.TaxDeclarationService;
import com.bishe.tax.service.TaxRegistrationService;
import com.bishe.tax.service.ToDoaskService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 纳税申报
 * <p>
 * <p>
 * <p>
 * <p>
 * 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Controller
@RequestMapping("/system/declaration")
public class TaxDeclarationController {
	private String prefix = "taxDeclaration";

	@Autowired
	private TaxDeclarationService declarationService;
	@Autowired
	private TaxRegistrationService taxRegistrationService;
	@Autowired
	private ToDoaskService tasksService;

	@GetMapping("list")
	public String declaration() {
		return prefix + "/list";
	}

	@RequestMapping("infoList")
	public String infoList() {
		return prefix + "/trList";
	}

	/**
	 * 查询纳税申报 列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TaxDeclaration declaration,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<TaxDeclaration> list = declarationService.selectDeclarationList(declaration);
		return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
	}

	/**
	 * 新增纳税申报
	 */
	@GetMapping("/add")
	public String add(
			@RequestParam(value = "txId" ) Integer txId, ModelMap mmap) {
		TaxRegistration taxRegistration = taxRegistrationService.selectRegistrationById(txId);
		mmap.put("txId",txId);
		mmap.put("company",taxRegistration.getTxCompany());
		mmap.put("owner",taxRegistration.getTxOwner());
		return prefix + "/add";
	}

	/**
	 * 新增保存纳税申报
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TaxDeclaration declaration,
							  @RequestParam(value = "txId" ) Integer txId) {
		TaxRegistration taxRegistration = taxRegistrationService.selectRegistrationById(txId);
		if (declaration.getDeclarationTaxType().equals("个人所得税")){
			declaration.setOwner(taxRegistration.getTxOwner());
		}else{
			declaration.setOwner("");
		}
		declaration.setCompany(taxRegistration.getTxCompany());
		declaration.setStatus(1);
		declaration.setTaxId(txId);
		return AjaxResult.success(declarationService.insertDeclaration(declaration));
	}

	/**
	 * 修改纳税申报
	 */
	@GetMapping("/edit/{declarationId}")
	public String edit(@PathVariable("declarationId") Integer declarationId,
			@RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
		TaxDeclaration declaration = declarationService.selectDeclarationById(declarationId);
		mmap.put("taxDeclaration", declaration);
		mmap.put("taskId", taskId);
		return prefix + "/edit";
	}

	/**
	 * 修改保存纳税申报
	 */
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TaxDeclaration declaration,
			@RequestParam(value = "taskId", required = false) String taskId) {
		if (StringUtils.isNotBlank(taskId)) {
			tasksService.doComplete(taskId, null);
			declaration.setStatus(2);
		}
		return AjaxResult.success(declarationService.updateDeclaration(declaration));
	}

	/**
	 * 删除纳税申报
	 */
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return AjaxResult.success(declarationService.deleteDeclarationByIds(ids));
	}
	
	
	@GetMapping("examine/{taskId}/{declarationId}")
	public String doExamine(@PathVariable("declarationId") String declarationId, @PathVariable("taskId") String taskId,
			ModelMap mmap) {
		TaxDeclaration declaration = declarationService.selectDeclarationById(Integer.valueOf(declarationId));
		mmap.put("taxDeclaration", declaration);
		mmap.put("taskId", taskId);
		return prefix + "/taxDeclaration";
	}

	/**
	 * 审核
	 *
	 * @param taskId
	 * @param flag
	 */
	@PostMapping("complete")
	@ResponseBody
	public AjaxResult doComplete(TaxDeclaration declaration, @RequestParam("taskId") String taskId,
			@RequestParam("flag") Boolean flag) {
		tasksService.doComplete(taskId, flag);
		if (flag) {
			declaration.setStatus(4);
		} else {
			declaration.setStatus(3);
		}
		return AjaxResult.success(declarationService.updateDeclaration(declaration));
	}

	/**
	 * 提交审核
	 *
	 * @return
	 */
	@PostMapping("submitFlow")
	@ResponseBody
	public AjaxResult submitFlow(@RequestParam("declarationId") String declarationId, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		return AjaxResult.success(declarationService.doSubmitFlow(user, declarationId));
	}

}
